package com.competitions.videoedithelper.utils;

import com.competitions.videoedithelper.entity.PageBean;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 盛旭雯
 */
public class PageUtil {

    public static JSONObject wrap(List<Map<String,Object>> data,String title,int pageSize,int pageNum){
        PageBean<Map<String,Object>> pb = new PageBean<>();
        int total = data.size();
        pb.setTr(total);
        pb.setPs(pageSize);
        pb.setPc(pageNum);
        List<Map<String,Object>> nowList = new ArrayList<>();
        for(int i = (pageNum-1)*pageSize;i<pageNum*pageSize;i++){
            //每行ps个记录数，i最大只能循环ps次，当i+1大于数组的长度时，说明数组已经到头
            if(i<data.size()){
                data.get(i).put("generTime",data.get(i).get("generTime").toString().substring(0,19));
                nowList.add(data.get(i));
            }else {
                break;
            }
        }
        pb.setBeanList(nowList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(title,nowList);
        jsonObject.put("pageNum",pb.getPc());
        jsonObject.put("pageSize",pb.getPs());
        jsonObject.put("total",total);
        return jsonObject;
    }
}
